package com.example.controller;

import com.example.entity.Admin;
import com.example.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class Loginctrl {
    @Resource
    AdminService adminService;

    @GetMapping("/toLoginPage")
    public String toLogin2(){
        return "User";
    }

//    @RequestMapping("/doLogin")

//    public String toLogin(){
//        return "redirect:/goods";
//    }

    @PostMapping("/doLogin")
    public String doLogin(Admin admin, Model model,HttpSession session) {
        System.out.println(admin.getAduname() + "--=+++--" + admin.getAdupwd());

        // 用户名 密码进行验证
        Admin user = adminService.getAdminUser(admin);
        if (user == null) {
            model.addAttribute("msg", "用户或密码不正确");
            return "User";
        } else {
            session.setAttribute("uName", admin.getAduname());
            System.out.println(session.getAttribute("uName"));
            System.out.println(session);
            return "redirect:/admin/goods";

        }
    }
@GetMapping("/logout")
    public String doLogout(HttpSession session, SessionStatus sessionStatus){
        // 只是让session失效 但session仍然存在
        session.invalidate();
        sessionStatus.setComplete();
        return "login";
    }


    public String doLogin(String aduName,@RequestParam("aduPass") String aduPas){
        System.out.println(aduName + "----"+aduPas);
        return "User";
    }
}
